package com.learn.educommentboot.controller;

import com.learn.eduapi.entity.ResponseDTO;
import com.learn.eduapi.entity.comment.CourseComment;
import com.learn.eduapi.entity.comment.CourseCommentFavoriteRecord;
import com.learn.educommentboot.mapper.CourseCommentDao;
import com.learn.educommentboot.service.CourseCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 课程留言
 */
@RestController
@CrossOrigin
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CourseCommentService courseCommentService;
    @Autowired
    private CourseCommentDao mapper;

    /**
     * 获取课程的留言
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseCommentList/{courseId}/{pageNum}/{pageSize}/{userId}")
    public ResponseDTO<List<CourseComment>> getCourseCommentList(@PathVariable("courseId")Integer courseId,
                                                                 @PathVariable("pageNum")Integer pageNum,
                                                                 @PathVariable("pageSize")Integer pageSize,
                                                                 @PathVariable("userId")Integer userId){
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(courseCommentService.selectgetAllComment(userId,courseId,(pageNum-1)*pageSize,pageSize));
        responseDTO.setState(courseCommentService.selectgetAllComment(userId,courseId,0,0).size());
        responseDTO.setMessage("获取留言信息成功");
        return responseDTO;
    }


    /**
     * 发表留言
     * @return
     */
    @GetMapping("/saveCourseComment")
    public ResponseDTO<List<CourseComment>> saveCourseComment(
            @RequestParam("courseId") Integer courseId,
            @RequestParam("userId") Integer userId,
            @RequestParam("userName") String userName,
            @RequestParam("comment") String comment
    ){
        System.out.println("comment = " + comment);
        CourseComment courseComment = new CourseComment();
        courseComment.setCourseId(courseId);
        courseComment.setUserId(userId);
        courseComment.setUserName(userName);
        courseComment.setComment(comment);
        Date date = new Date();
        //Date类型转String  //可指定任意的返回格式  yyyy
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        //创建时间
        courseComment.setCreateTime(dateString);
        //跟新时间
        courseComment.setUpdateTime(dateString);
        courseCommentService.addComment(courseComment);
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        responseDTO.setMessage("留言成功");
        responseDTO.setState(200);
        return responseDTO;
    }

    /**
     * 点赞
     * @param commentId
     * @param userId
     * @return
     */
    @GetMapping("saveFavorite/{commentId}/{userId}")
    public ResponseDTO<List<CourseComment>> saveFavorite(@PathVariable("commentId")Integer commentId,
                                                         @PathVariable("userId")Integer userId){
        Date date = new Date();
        //Date类型转String  //可指定任意的返回格式  yyyy
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        CourseCommentFavoriteRecord courseCommentFavoriteRecord = mapper.selectByCourseCommentFavoriteRecordId(userId, commentId);
        if (courseCommentFavoriteRecord==null){
            CourseCommentFavoriteRecord newFavoriteRecord = new CourseCommentFavoriteRecord();
            newFavoriteRecord.setUserId(userId);
            newFavoriteRecord.setCommentId(commentId);
            newFavoriteRecord.setCreateTime(dateString);
            newFavoriteRecord.setUpdateTime(dateString);
            courseCommentService.OneaddCommentFavorite(newFavoriteRecord);
        }else {
            int i1 = courseCommentService.updateCommentSave(dateString, commentId, userId);
        }
        Integer i = courseCommentService.selectLikeCount(commentId)+1;
        courseCommentService.updateLikeCount(i,commentId);
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        responseDTO.setMessage("点赞成功");
        responseDTO.setState(200);
        return responseDTO;
    }

    /**
     * 取消赞
     * @param commentId
     * @param userId
     * @return
     */
    @GetMapping("cancelFavorite/{commentId}/{userId}")
    public ResponseDTO<List<CourseComment>> cancelzan(@PathVariable("commentId")Integer commentId,
                                                         @PathVariable("userId")Integer userId){
        System.out.println("取消赞");
        Date date = new Date();
        //Date类型转String  //可指定任意的返回格式  yyyy
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        courseCommentService.updateComment(dateString,commentId,userId);
        Integer i = courseCommentService.selectLikeCount(commentId) -1;
        courseCommentService.updateLikeCount(i,commentId);
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        responseDTO.setMessage("取消赞成功");
        responseDTO.setState(200);
        return responseDTO;
    }

    /**
     * 取消赞
     * @param commentId
     * @return
     */
    @GetMapping("delComment/{commentId}")
    public ResponseDTO<List<CourseComment>> delComment(@PathVariable("commentId")Integer commentId){
        System.out.println("删除留言");
        int i = courseCommentService.delComment(commentId);
        ResponseDTO<List<CourseComment>> responseDTO = new ResponseDTO<>();
        responseDTO.setMessage("删除留言");
        responseDTO.setState(200);
        return responseDTO;
    }

}
